package com.cspm.ahjy.bc.office.word.util;

import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Element;

import com.cspm.ahjy.bc.office.word.domain.MSParagraph;
import com.cspm.ahjy.bc.office.word.domain.MSTableCol;

/**
 * 表格列工具类
 * @author ServerZhang
 * @date 2017年10月12日
 */
public class TableColUtil {

	/**
	 * 解析<w:tc>标签
	 * @param tcElement
	 * @param path 文档所在目录 
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月12日
	 */
	public static MSTableCol getMSTableCol(Element tcElement, String path){
		MSTableCol msTableCol = new MSTableCol();
		
		if (tcElement == null) {
			return msTableCol;
		}
		
		//1.列属性 
		Element tcPr = tcElement.element("tcPr");
		if (tcPr != null) {
			Element vAlign = tcPr.element("vAlign");
			if (vAlign != null) {
				Attribute val = vAlign.attribute("val");
				if (val != null) {
					msTableCol.setAlignType(val.getText());
				}
			}
			Element tcW = tcPr.element("tcW");
			if (tcW != null) {
				Attribute w = tcW.attribute("w");
				if (w != null) {
					msTableCol.setWidth(Integer.parseInt(w.getText()));
				}
			}
		}
		
		//2.获取列
		Element pElement = tcElement.element("p");
		Map<String, List<MSParagraph>> paragraph = ParagraphUtil.getParagraph(pElement, 0, path);
		List<MSParagraph> list = paragraph.get("paragraph");
		if (list.size() > 0) {//只取第一段内容
			MSParagraph msParagraph = list.get(0);
			String text = msParagraph.getText();
			String alignment = msParagraph.getAlignment();//5种对齐方式，这里只应该出现3种，left,right,center
			if ("double".equals(alignment)) {//段落的默认方式在这里处理为左对齐
				alignment = "left";
			}
			msTableCol.setHorizontalType(alignment);
			if (text != null) {
				msTableCol.setText(text);
			}
		}
		return msTableCol;
	}
}
